﻿<%@ Page Language="c#" ContentType="text/html" ResponseEncoding="utf-8" Async="true" MasterPageFile="~/administration/master_popup.master" Inherits="ACM.cmsarea08.Administration.Core.OptPageBase" %>
<%@ MasterType VirtualPath="~/administration/master_popup.master" %>



<asp:Content ID="PageBody" Runat="server" ContentPlaceHolderID="MasterBody">
<div id="option_popup">
<div class="popup_msgs">

</div>
<div class="popup_input">
<asp:DropDownList ID="dropcartelle" runat="server"></asp:DropDownList>
<asp:LinkButton ID="sposta" Text="Prosegui" runat="server" OnClick="Sposta"></asp:LinkButton>
</div>
<input type="hidden" id="newvalue" runat="server"  />
</div>
</div>
<script language="c#" runat="server">
private string cartella = "";
private string qs = "";
protected override void OnInit(EventArgs e){
	base.OnInit(e);
	CaricaDati();


}
private void CaricaDati(){
	if(HttpUtility.HtmlDecode(Request.QueryString["files"])==null || HttpUtility.HtmlDecode(Request.QueryString["folder"])==null){
		Response.Write("Errore server");
		return;
	}
	qs = HttpUtility.HtmlDecode(Request.QueryString["files"].ToString());
	qs = qs.StartsWith("|")?qs.Substring(1,qs.Length-1):qs;
	
	cartella = Server.MapPath("/public/media/" + HttpUtility.HtmlDecode(Request.QueryString["folder"].ToString()));
	//for(int i=0;i<files.Length;i++){
//		Response.Write(files[i] + "<hr>");
//	}
//	Response.Write(files.Length.ToString() + " tutti da " + cartella );
	System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath("/public/media"));
    if (!di.Exists)
    {
        Response.Write("Errore: la cartella Media non esiste");
        return;
    }
	dropcartelle.Items.Clear();
	CostruisciDrop(di,0);
	
	
}

private void CostruisciDrop(System.IO.DirectoryInfo di, int depth){
   // Response.Write("<hr>cecco " + di.FullName + "\\ con " + cartella);
    if (di.FullName + "\\" != cartella)
        dropcartelle.Items.Add(new ListItem(di.Name.PadLeft((di.Name.Length + depth * 2), '-'), di.FullName));
	foreach(System.IO.DirectoryInfo diri in di.GetDirectories()){
       
	//	Response.Write("<hr>" + depth.ToString() + diri.Name.PadLeft((diri.Name.Length+depth*2)));
		CostruisciDrop(diri,depth+1);
		
		
		
		
	}


}

private void Page_Load(Object o, EventArgs e){
	
	
}

    private void Sposta(Object o, EventArgs e)
    {
    string[] files = HttpUtility.UrlDecode(qs.Replace("*", ".")).Split('|');
	string dest = dropcartelle.SelectedValue+"\\";
//	Response.Write("<hr><hr>Allora.. devo spostare " + qs.Replace("*",".").Replace("|"," e ") + "<br> da " + cartella + " a " + dest + "<br>ma lo faccio domani...");
    for (int i = 0; i < files.Length; i++)
    {
        if (files[i].Trim() == "")
            continue;
        Response.Write("<br>sposto " + files[i] + "<br>da " + cartella + " a " + dest + ".....");
        if (!files[i].StartsWith(":"))
        {
            //Response.Write("sono una cartella --> " + files[i].Substring(1));
            if (SpostaFile(files[i], cartella, dest) > 0)
            {
                Response.Write("Fatto!<hr>");
            }
            else
            {
                Response.Write("fallito<hr>");
            }
        }
        else
        {
            //Response.Write("sono un file --> " + files[i]);
            if (SpostaCartella(files[i].Replace(":",""), cartella, dest) > 0)
            {
                Response.Write("Fatto!<hr>");
            }
            else
            {
                Response.Write("fallito<hr>");
            }
        }
        
    }
}

private int SpostaFile(string filename,string from,string to){
    int reso = 0;
    
    System.IO.FileInfo fi = new System.IO.FileInfo(from + filename);
    if (fi.Exists)
    {
       // Response.Write("----" + fi.FullName +"----" + to+filename);
        fi.MoveTo(to+filename);
        reso++;
    }
    return reso;
}
private int SpostaCartella(string folder, string from, string to)
    {
        int reso = 0;
        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(from + folder);
        if (di.Exists)
        {
            di.MoveTo(to + folder);
            reso++;
        }
        return reso;
    }
</script>




</asp:content>
